/**
 * Created by Administrator on 2017/3/21.
 */
import React, {Component} from 'react';
import {
  Form, Input, Radio, Select, Icon,
  Upload, Tooltip, DatePicker, Button,
} from 'antd';
import {connect} from 'dva';
import ReactDataGrid from 'react-data-grid';
import moment from 'moment'
import {download,renderReport} from '@/utils/utils';
import config from '@/config/config'
import find from 'lodash/find';
import filter from 'lodash/filter';

const FormItem = Form.Item;
const { TextArea } = Input;
const RadioGroup = Radio.Group;
const Option = Select.Option;

class AddPoliciesForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      users:[],
      parseData:[],
      parseData2:[],
      columns:[],
      columns2:[]
    };
  }

  componentDidMount() {
    const that=this;
    let arr=[
      {name:'流动资产:',key:'zc1',line:'',offset:'0'},
      {name:'货币资金',key:'zc2',line:'1',offset:'1'},
      {name:'短期投资',key:'zc3',line:'2',offset:'1'},
      {name:'应收票据',key:'zc4',line:'3',offset:'1'},
      {name:'应收账款',key:'zc5',line:'4',offset:'1'},
      {name:'减:坏账准备',key:'zc6',line:'5',offset:'2'},
      {name:'应收账款净额',key:'zc7',line:'6',offset:'1'},
      {name:'预付账款',key:'zc8',line:'7',offset:'1'},
      {name:'应收股利',key:'zc9',line:'8',offset:'1'},
      {name:'其他应收款',key:'zc10',line:'9',offset:'1'},
      {name:'存款',key:'zc11',line:'10',offset:'1'},
      {name:'其中:原材料',key:'zc12',line:'11',offset:'2'},
      {name:'库存商品',key:'zc13',line:'',offset:'3'},
      {name:'待摊费用',key:'zc14',line:'12',offset:'1'},
      {name:'待处理流动资产净损失',key:'zc15',line:'13',offset:'1'},
      {name:'一年内到期的长期债券投资',key:'zc16',line:'14',offset:'1'},
      {name:'其它流动资产:',key:'zc17',line:'15',offset:'1'},
      {name:'流动资产合计',key:'zc18',line:'20',offset:'0'},
      {name:'非流动资产',key:'zc19',line:'',offset:'0'},
      {name:'长期投资',key:'zc20',line:'21',offset:'1'},
      {name:'固定资产原价',key:'zc21',line:'24',offset:'1'},
      {name:'减:累计折旧',key:'zc22',line:'25',offset:'2'},
      {name:'固定资产净值',key:'zc23',line:'26',offset:'1'},
      {name:'在建工程',key:'zc24',line:'28',offset:'1'},
      {name:'固定资产清理',key:'zc25',line:'27',offset:'1'},
      {name:'长期待摊费用',key:'zc26',line:'29',offset:'1'},
      {name:'无形资产',key:'zc27',line:'36',offset:'1'},
      {name:'减:累计摊销',key:'zc28',line:'',offset:'1'},
      {name:'无形资产净额',key:'zc29',line:'35',offset:'1'},
      {name:'递延资产',key:'zc30',line:'37',offset:'1'},
      {name:'非流动资产合计',key:'zc31',line:'',offset:'0'},
      {name:'资产总计',key:'zc32',line:'45',offset:'0'},

    ]
    let arr2=[
      {name:'流动负债:',key:'fz1',line:'',offset:'0'},
      {name:'短期借款',key:'fz2',line:'51',offset:'1'},
      {name:'应付票据',key:'fz3',line:'52',offset:'1'},
      {name:'应付账款',key:'fz4',line:'53',offset:'1'},
      {name:'预收账款',key:'fz5',line:'54',offset:'1'},
      {name:'其他应付款',key:'fz6',line:'58',offset:'1'},
      {name:'应付职工薪酬',key:'fz7',line:'60',offset:'1'},
      {name:'其中:应付工资',key:'fz8',line:'61',offset:'2'},
      {name:'应付福利',key:'fz9',line:'',offset:'3'},
      {name:'应交税费',key:'fz10',line:'62',offset:'1'},
      {name:'其中:应交税金',key:'fz11',line:'',offset:'2'},
      {name:'未付利润',key:'fz12',line:'64',offset:'1'},
      {name:'预提费用',key:'fz13',line:'67',offset:'1'},
      {name:'代扣税金',key:'fz14',line:'68',offset:'1'},
      {name:'一年内到期的长期负债',key:'fz15',line:'69',offset:'1'},
      {name:'其它流动负债',key:'fz16',line:'70',offset:'1'},
      {name:'流动负债合计',key:'fz17',line:'75',offset:'0'},
      {name:'非流动负债:',key:'fz18',line:'',offset:'0'},
      {name:'长期借款',key:'fz19',line:'76',offset:'1'},
      {name:'长期应付款',key:'fz20',line:'77',offset:'1'},
      {name:'专项应付款',key:'fz21',line:'78',offset:'1'},
      {name:'其它长期负债',key:'fz22',line:'79',offset:'1'},
      {name:'长期负债合计',key:'fz23',line:'83',offset:'1'},
      {name:'负债合计',key:'fz24',line:'',offset:'1'},
      {name:'所有者权益:',key:'fz25',line:'',offset:'0'},
      {name:'实收资本',key:'fz26',line:'86',offset:'1'},
      {name:'资本公积',key:'fz27',line:'93',offset:'1'},
      {name:'减:库存股',key:'fz28',line:'',offset:'2'},
      {name:'盈余公积',key:'fz29',line:'94',offset:'1'},
      {name:'未分配利润',key:'fz30',line:'96',offset:'1'},
      {name:'所有者权益合计',key:'fz31',line:'98',offset:'1'},
      {name:'负债及所有者权益总计',key:'fz32',line:'100',offset:'1'},
    ]

    let data=[]
    const items=filter(this.props.editRecord.items,function(o) {
      return o.name.indexOf('_2')<0
    })
    console.log('items',items)
    let items2=filter(this.props.editRecord.items,function(o) {
      return o.name.indexOf('_2')>=0
    })
    console.log('items2',items2)

    const columns = [
      { key: "name", name: "资产",width:200,resizable: true,
        formatter: (event) => {
          return   <div  style={{
            textIndent:Number(event.row.offset)*30,
            fontWeight:(event.row.offset==='0'||event.row.name.indexOf('合计')>=0||event.row.name.indexOf('总计')>=0)?'bold':'',
            textAlign:(event.row.name.indexOf('合计')>=0||event.row.name.indexOf('总计')>=0)?'center':'',
          }}>
            {event.value}
          </div>;
        },},
    ];
    for(let i=0;i<items.length;i++){
      columns.push(  { key: items[i].name, name: items[i].name,resizable: true,
        formatter: (event) => {
          return   <div  style={{
          }}>
            {event.value.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
          </div>;
        }},)
    }

    const columns2 = [
      { key: "name", name: "负债及所有者权益",width:200,resizable: true,
        formatter: (event) => {
          return   <div  style={{
            textIndent:Number(event.row.offset)*30,
            fontWeight:(event.row.offset==='0'||event.row.name.indexOf('合计')>=0||event.row.name.indexOf('总计')>=0)?'bold':'',
            textAlign:(event.row.name.indexOf('合计')>=0||event.row.name.indexOf('总计')>=0)?'center':'',
          }}>
            {event.value}
          </div>;
        },},
    ];
    for(let i=0;i<items2.length;i++){
      columns2.push(  { key: items2[i].name, name: items2[i].name.split('_')[0],resizable: true,
        formatter: (event) => {
          return   <div  style={{
          }}>
            {event.value.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
          </div>;
        }},)
    }
    console.log('columns',columns)
    console.log('columns2',columns2)
    this.setState({
      columns
    },function() {
      let newArr=[...arr]
      for(let i=0;i<newArr.length;i++){
        data[i]={name:newArr[i].name,key:newArr[i].key,offset:newArr[i].offset}
        for(let j=0;j<items.length;j++){
          let exit=find(items[j].items,function(o) {
            return o.key===newArr[i].key
          })
          if(exit){
            data[i][`${items[j].name}`]=exit.value
          }
        }
      }
      console.log('data',data)
      this.setState({
        parseData:data
      })
    })

    let data2=[]

    this.setState({
      columns2
    },function() {
      let newArr=[...arr2]
      for(let i=0;i<newArr.length;i++){
        data2[i]={name:newArr[i].name,key:newArr[i].key,offset:newArr[i].offset}
        for(let j=0;j<items2.length;j++){
          let exit=find(items2[j].items,function(o) {
            return o.key===newArr[i].key
          })
          if(exit){
            data2[i][`${items2[j].name}`]=exit.value
          }
        }
      }
      console.log('data2',data2)
      this.setState({
        parseData2:data2
      })
    })
  }
  render() {
    const formItemLayoutWithLabel = {
      labelCol: {
        xs: {span: 24},
        sm: {span: 11},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 6},
      }
    };
    return (
      <div>
        <h1 style={{textAlign:'center'}}>资产负债表</h1>
        <Form onSubmit={this.handleSubmit} style={{textAlign:'center'}}>
          <FormItem
            style={{display:'inline-block',width:'33%'}}
            {...formItemLayoutWithLabel}
            label={(
              <span>
               月份
            </span>
            )}
          >
            <span style={{fontWeight:'bold'}}>{this.props.editRecord.customize.month}</span>
          </FormItem>
        </Form>
        <div>
          <div style={{display:'inline-block',width:'50%'}}>
            <ReactDataGrid
              columns={this.state.columns}
              rowGetter={i => this.state.parseData[i]}
              rowsCount={this.state.parseData.length}

              minHeight={this.state.parseData.length*35+36}
            />
          </div>
          <div style={{display:'inline-block',width:'50%'}}>
            <ReactDataGrid
              columns={this.state.columns2}
              rowGetter={i => this.state.parseData2[i]}
              rowsCount={this.state.parseData2.length}

              minHeight={this.state.parseData.length*35+36}
            />
          </div>
        </div>

        <Form style={{marginTop:'10px'}} onSubmit={this.handleSubmit}>
          <FormItem
            style={{display:'inline-block',width:'33%'}}
            {...formItemLayoutWithLabel}
            label={(
              <span>
               部门负责人
            </span>
            )}
          >
            <span style={{fontWeight:'bold'}}>{this.props.editRecord.customize.director}</span>

          </FormItem>
          <FormItem
            style={{display:'inline-block',width:'33%'}}
            {...formItemLayoutWithLabel}
            label={(
              <span>
               制表
            </span>
            )}
          >
            <span style={{fontWeight:'bold'}}>{this.props.editRecord.customize.author}</span>

          </FormItem>
          {
            this.props.editRecord.original_files.length>0&&
            <FormItem
              style={{display:'inline-block',width:'33%'}}
              {...formItemLayoutWithLabel}
              label={'原文件'}
            >
              <div onClick={()=>{
                download(`${config.prefix}/reports/${this.props.editRecord.id}/original_files/${this.props.editRecord.original_files[0].key}/download??token=Bearer ${sessionStorage.getItem('token')}`)
              }} title={'点击下载'} style={{color:'blue',cursor:'pointer',textOverflow:'hidden',whiteSpace:'nowrap',overflow:'hidden'}}>
                {this.props.editRecord.original_files[0].name}
              </div>
            </FormItem>
          }
        </Form>
      </div>
    );
  }
}

const AddPoliciesFormWrap = Form.create()(AddPoliciesForm);
export default connect()(AddPoliciesFormWrap);


